package cn.liutao.order.api;

import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.order.entity.param.OrderQueryParam;
import cn.liutao.order.entity.vo.OrderVo;
import cn.liutao.order.service.OrderService;
import com.mybatisflex.core.paginate.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author liutao
 * @Description 订单order
 * @date 2023/11/2 9:28
 * @Version 1.0
 */
@WebServlet("/order")
public class OrderServlet extends HttpServlet {

    private OrderService orderService;

    @Override
    public void init() throws ServletException {
        orderService = OrderService.getInstance();
    }

    /**
     * 查询订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderQueryParam queryParam = ServletUtils.getQueryParam(req, OrderQueryParam.class);
        Integer userId = LoginUser.getUserId();
        queryParam.setUserId(userId);
        Page<OrderVo> page  = orderService.selectList(queryParam);
        ServletUtils.ok(resp,page);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }

    /**
     * 删除订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)  {
        List<String> ids = ServletUtils.getQueryParams(req, "id");
        Integer userId = LoginUser.getUserId();
        for (String id : ids) {
            orderService.removeById(Integer.valueOf(id));
        }
        ServletUtils.ok(resp,"删除成功");
    }
}
